package com.ruoyi.project.ins.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsConversation;
import com.ruoyi.project.ins.service.IInsConversationService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/ins/conversation")
public class InsConversationController extends BaseController {
    @Autowired
    private IInsConversationService iInsConversationService;

    @PreAuthorize("@ss.hasPermi('ins:conversation:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsConversation insConversation)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insConversation.setCreateBy(username);
        }
        startPage();
        List<InsConversation> list = iInsConversationService.selectInsConversationList(insConversation);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ins:conversation:add')")
    @Log(title = "ins话术管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InsConversation insConversation)
    {
        insConversation.setCreateBy(getUsername());
        insConversation.setStatus("0");
        return toAjax(iInsConversationService.insertInsConversation(insConversation));
    }

    @PreAuthorize("@ss.hasPermi('ins:conversation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(iInsConversationService.selectInsConversationById(id));
    }

    @PreAuthorize("@ss.hasPermi('ins:conversation:edit')")
    @Log(title = "ins话术管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsConversation insConversation)
    {
        insConversation.setUpdateBy(getUsername());
        return toAjax(iInsConversationService.updateInsConversation(insConversation));
    }
}
